

<script type="text/javascript" src="{{asset('/static/js/jquery.disappear.tooltip.js')}}"></script>
<script type="text/javascript">
    // 选择注册个人
    $('#J_reg_select_per').die().live('click', function () {
        $('.J_reg_select_box').hide();
        $('.J_u_type').val('2');
        $('.J_top_reg_type_txt').html('个人');
        $('.J_top_change_reg_type').html('切换为企业注册');
        $('.J_reg_com_box').hide();
        $('.J_do_box').show();
    });
    // 选择注册企业
    $('#J_reg_select_com').die().live('click', function () {
        $('.J_reg_select_box').hide();
        $('.J_u_type').val('1');
        $('.J_top_reg_type_txt').html('企业');
        $('.J_top_change_reg_type').html('切换为个人注册');
        $('.J_reg_com_box').show();
        $('.J_do_box').show();
    });
    // 切换注册类型
    $('.J_top_change_reg_type').die().live('click', function () {
        if ($('.J_u_type').val() == '1') {
            $('.J_top_reg_type_txt').html('个人');
            $('.J_top_change_reg_type').html('切换为企业注册');
            $('.J_u_type').val('2');
            $('.J_reg_com_box').hide();
        } else {
            $('.J_top_reg_type_txt').html('企业');
            $('.J_top_change_reg_type').html('切换为个人注册');
            $('.J_u_type').val('1');
            $('.J_reg_com_box').show();
        }
    });
    // 注册时检测手机号是否存在

    $('.J_reg_per_box input[name=mobile]').keyup(function () {
        let currentValue = $(this).val();
        if (currentValue.length == 11) {
            if (regularMobile.test(currentValue) && !remoteValid(currentValue, this)) {
                $('.J_top_has_remind').show();
                $('#J_top_get_code').addClass('btn_disabled');
            } else {
                $('.J_top_has_remind').hide();
                $('#J_top_get_code').removeClass('btn_disabled');
            }
        } else {
            $('.J_top_has_remind').hide();
            $('#J_top_get_code').removeClass('btn_disabled');
        }
    });

    // 检测账号是否存在
    function remoteValid(value, element) {
        var result = false, eletype = element.name;
        $.ajax({
            url: '{{url('auth/check')}}',
            cache: false,
            async: false,
            type: 'post',
            dataType: 'json',
            data: {type: eletype, param: value},
            success: function (json) {
                if (json && json.status) {
                    result = true;
                } else {
                    result = false;
                }
            }
        });
        return result;
    }

    // 获取验证码
    $('#J_top_get_code').die().live('click', function () {
        if ($(this).hasClass('btn_disabled')) {
            return false;
        }
        let mobileValue = $.trim($('.J_reg_per_box input[name=mobile]').val());
        if (mobileValue == '') {
            disapperTooltip("remind", "请输入手机号码");
            console.log("请输入手机号码");
            $('.J_reg_per_box input[name=mobile]').focus();
            return false;
        }
        if (mobileValue != "" && !regularMobile.test(mobileValue)) {
            disapperTooltip("remind", "请输入正确的手机号码");
            $('.J_reg_per_box input[name=mobile]').focus();
            console.log("请输入正确的手机号码");
            return false;
        }
        topSendSms();
    });

    // 发送验证码
    function topSendSms() {
        //$('#J_top_get_code').text('发送中...').addClass('btn_disabled');
        let mobile = $.trim($('.J_reg_per_box input[name=mobile]').val());
        $.ajax({
            url: '{{url('auth/sms')}}',
            cache: false,
            async: false,
            type: 'post',
            dataType: 'json',
            data: {mobile: mobile},
            success: function (result) {
                if (result.status) {
                    disapperTooltip("success", "验证码已发送，请注意查收");
                    // 开始倒计时
                    let countdown = 180;

                    function setTime() {
                        if (countdown == 0) {
                            $('#J_top_get_code').text('获取验证码').removeClass('btn_disabled');
                            countdown = 180;
                            return;
                        } else {
                            $('#J_top_get_code').text('重新发送' + countdown + '秒').addClass('btn_disabled');
                            countdown--;
                        }
                        setTimeout(function () {
                            setTime()
                        }, 1000)
                    }
                    setTime();
                } else {
                    $('#J_top_get_code').text('获取验证码').removeClass('btn_disabled');
                    disapperTooltip("remind", result.msg);
                }
            }
        });
    }

    // 注册验证
    $('#J_top_reg_btn').die().live('click', function () {
        let mobileValue = $.trim($('.J_reg_per_box input[name=mobile]').val());
        let mobileCodeValue = $.trim($('.J_reg_per_box input[name=mobile_vcode]').val());
        if (mobileValue == '') {
            disapperTooltip("remind", "请输入手机号码");
            $('.J_reg_per_box input[name=mobile]').focus();
            return false;
        }
        if (mobileValue != "" && !regularMobile.test(mobileValue)) {
            disapperTooltip("remind", "请输入正确的手机号码");
            $('.J_reg_per_box input[name=mobile]').focus();
            return false;
        }
        if (mobileCodeValue == '') {
            disapperTooltip("remind", "请输入手机验证码");
            $('.J_reg_per_box input[name=mobile_vcode]').focus();
            return false;
        }
        let dataValue = {
            mobile: mobileValue,
            mobile_code: mobileCodeValue,
            type: $('.J_u_type').val(),
            reg_type: $('.J_reg_per_box input[name=reg_type]').val()
        };
        if ($('.J_u_type').val() == '1') {
            let companyNameValue = $.trim($('.J_reg_per_box input[name=companyname]').val());
            let contactValue = $.trim($('.J_reg_per_box input[name=contact]').val());
            if (companyNameValue == '') {
                disapperTooltip("remind", "请输入企业名称");
                $('.J_reg_per_box input[name=companyname]').focus();
                return false;
            }
            if (contactValue == '') {
                disapperTooltip("remind", "请输入企业联系人");
                $('.J_reg_per_box input[name=contact]').focus();
                return false;
            }
            dataValue = {
                mobile: mobileValue,
                mobile_code: mobileCodeValue,
                type: $('.J_u_type').val(),
                reg_type: $('.J_reg_per_box input[name=reg_type]').val(),
                company_name: companyNameValue,
                contact: contactValue
            };
        }

        var password = $.trim($('.J_passwordalert_group input[name=password]').val());
        var passwordVerify = $.trim($('.J_passwordalert_group input[name=passwordVerify]').val());
        if (password == '') {
            disapperTooltip("remind", "请输入帐户密码");
            $('.J_passwordalert_group input[name=password]').focus();
            return false;
        }
        if (passwordVerify == '') {
            disapperTooltip("remind", "请输入确认密码");
            $('.J_passwordalert_group input[name=passwordVerify]').focus();
            return false;
        }
        if (password.length < 6 || password.length > 16) {
            disapperTooltip("remind", "密码长度要求为6-16个字符");
            $('.J_passwordalert_group input[name=password]').focus();
            return false;
        }
        if (password !== passwordVerify) {
            disapperTooltip("remind", "两次输入的密码不一致");
            $('.J_passwordalert_group input[name=passwordVerify]').focus();
            return false;
        }
        dataValue['password'] = password;
        dataValue['passwordVerify'] = passwordVerify;
        $('#J_top_reg_btn').val('注册中...').prop('disabled', !0);
        $.ajax({
            url: '{{url('auth/add')}}',
            type: 'POST',
            dataType: 'json',
            data: dataValue,
            success: function (data) {
                if (data.status === 1) {
                    window.location.href = data.data.url;
                } else {
                    $('#J_top_reg_btn').val('立即注册').prop('disabled', 0);
                    disapperTooltip("remind", data.msg);
                }
            },
            error: function (data) {
                $('#J_top_reg_btn').val('立即注册').prop('disabled', 0);
                disapperTooltip("remind", data.msg);
            }
        });
        return false;
    })
    // 直接登录
    $('.J_top_reg_user_log').die().live('click', function () {
        $(".modal,.modal_backdrop").remove();
        siteLoginModelShow();
    })
    $('.J_btncancel').die().live('click', function () {
        $(".modal,.modal_backdrop").remove();
    });
</script>